﻿using NursingFormSystem.Model;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace NursingFormSystem.NeonatalObservationTable
{
    public partial class HistoryNeonatalObservationTable : Form
    {
        #region 变量声明
        public NursingFormSystem.NurFormsService.NursingForm formService = new NursingFormSystem.NurFormsService.NursingForm();
        Tb_User LoginUser;
        PatientInfo PatientInfo;
        #endregion

        #region 构造函数
        public HistoryNeonatalObservationTable(Tb_User loginUser, PatientInfo patientInfo)
        {
            InitializeComponent();
            //成员变量
            LoginUser = loginUser;
            PatientInfo = patientInfo;
            BindControl();
            BindInfo();
        }
        #endregion

        #region 查询
        private void btnchaxun_Click(object sender, EventArgs e)
        {
            BindInfo();
        }
        #endregion

        #region 新增
        private void btnxinzeng_Click(object sender, EventArgs e)
        {
            AddNeonatalObservationTable addNO = new AddNeonatalObservationTable(LoginUser, PatientInfo, string.Empty);
            addNO.ShowDialog();
            if (addNO.DialogResult == DialogResult.OK)
            {
                BindInfo();
            }
        }
        #endregion

        #region 修改
        private void Btnxiugai_Click(object sender, EventArgs e)
        {
            if (bandedGridView1.RowCount > 0)
            {
                string Id = bandedGridView1.GetRowCellValue(bandedGridView1.FocusedRowHandle, "Id").ToString();
                AddNeonatalObservationTable addNOT = new AddNeonatalObservationTable(LoginUser, PatientInfo, Id);
                addNOT.ShowDialog();
                if (addNOT.DialogResult == DialogResult.OK)
                {
                    BindInfo();
                }
            }
        }
        #endregion

        #region 打印
        private void simpleButton1_Click(object sender, EventArgs e)
        {
            PrintNeonatalObservationTable pt = new PrintNeonatalObservationTable(LoginUser, PatientInfo, StatDate.DateTime, EndDate.DateTime);
            pt.ShowDialog();
        }
        #endregion

        #region 删除
        private void simpleButton2_Click(object sender, EventArgs e)
        {
            if (bandedGridView1.RowCount > 0)
            {
                if (MessageBox.Show("确定要删除数据吗？", "删除前确认", MessageBoxButtons.OKCancel) == DialogResult.OK)
                {
                    string id = bandedGridView1.GetRowCellValue(bandedGridView1.FocusedRowHandle, "Id").ToString();
                    var ret2 = formService.DeleteNursingFormRecord(id);
                    MessageBox.Show(ret2.M);
                    if (ret2.Code == 1)
                    {
                        string StatDate = this.StatDate.Text;
                        string EndDate = this.EndDate.Text;
                        BindInfo();
                    }
                }
            }
        }
        #endregion

        #region 查询共用方法

        #region 开始日期：当天-30天，结束日期：为当天日期
        public void BindControl()
        {
            this.StatDate.DateTime = DateTime.Now.AddDays(-30);
            this.EndDate.DateTime = DateTime.Now;
        }
        #endregion
        
        public void BindInfo()
        {
            if (this.PatientInfo == null) return;
            string patientid = this.PatientInfo.PATIENT_ID;        //住院号
            int VisitId = this.PatientInfo.VISIT_ID;         //住院次数
            int FormId = (int)FormType.BabyBornObserve;          //表单ID
            DataTable dt = new DataTable();
            dt.Columns.Add("RiQi", typeof(string));               //日期
            dt.Columns.Add("ShiJian", typeof(string));            //时间
            dt.Columns.Add("ChenJianHuLi", typeof(string));       //晨间护理
            dt.Columns.Add("ChiNaiLiang", typeof(string));        //吃奶量
            dt.Columns.Add("JiaNai", typeof(string));             //加奶
            dt.Columns.Add("YuNai", typeof(string));              //余奶 
            dt.Columns.Add("MuRu", typeof(string));               //母乳
            dt.Columns.Add("NiaoLiang", typeof(string));          //尿量
            dt.Columns.Add("NiaoSe", typeof(string));             //尿色
            dt.Columns.Add("FenLiang", typeof(string));           //粪量
            dt.Columns.Add("FenSe", typeof(string));              //粪色
            dt.Columns.Add("FenXingZhuang", typeof(string));      //粪性状
            dt.Columns.Add("WenXiang", typeof(string));           //温箱
            dt.Columns.Add("ShiDu", typeof(string));              //湿度
            dt.Columns.Add("LanGuangLiao", typeof(string));       //蓝光疗
            dt.Columns.Add("HR", typeof(string));                 //HR
            dt.Columns.Add("R", typeof(string));                  //R
            dt.Columns.Add("SPO", typeof(string));                //SPO
            dt.Columns.Add("BP", typeof(string));                 //BP
            dt.Columns.Add("TuNaiLiang", typeof(string));         //吐奶量
            dt.Columns.Add("QianMing", typeof(string));           //签名     
            dt.Columns.Add("Id", typeof(string));                 //ID隐藏，双击触发事件
            var ret = formService.GetNursingFormRecordByDate(patientid, VisitId, FormId, StatDate.DateTime, EndDate.DateTime);
            if (ret.Code == 1)
            {
                List<NursingFormRecord> list = Newtonsoft.Json.JsonConvert.DeserializeObject<List<NursingFormRecord>>(ret.D);
                list = list.OrderByDescending(p => p.AssessmentDate).ToList();
                foreach (NursingFormRecord record in list)
                {
                    var NursingFormData = record.NursingFormData;
                    NeonatalObservation nobn = new NeonatalObservation();
                    nobn = XmlUtil.XMLToObject<NeonatalObservation>(NursingFormData, Encoding.UTF8);
                    DataRow newRow = dt.NewRow();
                    newRow["RiQi"] = Convert.ToDateTime(nobn.RiQi).ToString("yyyy-MM-dd");     //日期
                    newRow["ShiJian"] = DateTimeHelper.HHmmssToHHmm(nobn.ShiJian);     //时间
                    newRow["ChenJianHuLi"] = nobn.ChenJianHuLi;         //晨间护理
                    newRow["ChiNaiLiang"] = nobn.ChiNaiLiang;           //吃奶量
                    newRow["JiaNai"] = nobn.JiaNai;                     //加奶
                    newRow["YuNai"] = nobn.YuNai;                       //余奶 
                    newRow["MuRu"] = nobn.MuRu;                         //母乳
                    newRow["NiaoLiang"] = nobn.NiaoLiang;               //尿量
                    newRow["NiaoSe"] = nobn.NiaoSe;                     //尿色
                    newRow["FenLiang"] = nobn.FenLiang;                 //粪量
                    newRow["FenSe"] = nobn.FenSe;                       //粪色
                    newRow["FenXingZhuang"] = nobn.FenXingZhuang;       //粪性状
                    newRow["WenXiang"] = nobn.WenXiang;                 //温箱
                    newRow["ShiDu"] = nobn.ShiDu;                       //湿度  
                    newRow["LanGuangLiao"] = nobn.LanGuangLiao;         //蓝光疗
                    newRow["HR"] = nobn.HR;                             //HR
                    newRow["R"] = nobn.R;                               //R
                    newRow["SPO"] = nobn.SPO;                           //SPO
                    newRow["BP"] = nobn.BP;                             //BP
                    newRow["TuNaiLiang"] = nobn.TuNaiLiang;             //吐奶量
                    newRow["QianMing"] = nobn.QianMing;                 //签名   
                    newRow["Id"] = record.Id;   //ID隐藏，双击触发事件
                    dt.Rows.Add(newRow);
                }
            }
            dt.DefaultView.Sort = "RiQi DESC,ShiJian DESC";
            DataTable data = dt.DefaultView.ToTable();
            gridControl1.DataSource = data;
        }
        #endregion

        #region 双击事件
        private void bandedGridView1_RowCellClick(object sender, DevExpress.XtraGrid.Views.Grid.RowCellClickEventArgs e)
        {
            if (e.Clicks == 2)
            {
                if (bandedGridView1.RowCount > 0)
                {
                    string Id = bandedGridView1.GetRowCellValue(bandedGridView1.FocusedRowHandle, "Id").ToString();
                    AddNeonatalObservationTable addN = new AddNeonatalObservationTable(LoginUser, PatientInfo, Id);
                    addN.ShowDialog();
                    if (addN.DialogResult == DialogResult.OK)
                    {
                        BindInfo();
                    }
                }
            }
        }
        #endregion
    }
}